/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.modules.api.service.impl.DriverInfoServiceImpl
 * <p>
 * 类产生时间: 2014-11-14 16:34
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 项目组 : 苏州公安移动门户
 * <p>
 * 部门: 手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.modules.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.oceansoft.mobile.econsole.common.util.MD5Util;
import com.oceansoft.mobile.econsole.common.util.encrypt.DesAPPUtils;
import com.oceansoft.mobile.econsole.modules.api.entity.DriverInfo;
import com.oceansoft.mobile.econsole.modules.api.entity.DriverInfos;
import com.oceansoft.mobile.econsole.modules.api.persistence.IDriverInfoMapper;
import com.oceansoft.mobile.econsole.modules.api.service.IDriverInfoService;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.jlapi.entity.DriverLicenseSearchCondition;
import com.oceansoft.mobile.econsole.modules.jlapi.entity.InQueryServiceInfo;
import com.oceansoft.mobile.econsole.modules.jlapi.service.IJlApiService;
import com.oceansoft.mobile.econsole.modules.member.entity.WtMember;
import com.oceansoft.mobile.econsole.modules.member.service.IWtMemberService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

import static org.springframework.util.StringUtils.hasText;

/**
 * 驾驶员信息业务组件处理类
 *
 * @author chenw
 * @time 2014-11-14 16:34
 */
@Service
@Transactional
public class DriverInfoServiceImpl implements IDriverInfoService {


    @Resource
    private IJlApiService jlApiService;
    @Resource
    private IWtMemberService wtMemberService;

    @Resource
    private IDriverInfoMapper driverInfoMapper;

    @Override
    public Result loadDriveBindInfo(String userId) {
        if (Strings.isNullOrEmpty(userId)) {
            return new Result(Result.FAILURE, "请选择要查询用户编号");
        }
        List<DriverInfos> list = driverInfoMapper.loadDriverBindInfo(userId);
        if (list.isEmpty())
            return new Result(Result.FAILURE, "暂无绑定信息");
        return new Result(list);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Result bindInfo(HttpServletRequest request) {
        DriverInfo driverInfo = new DriverInfo();
        String guid=request.getParameter("guid");
        int id=0;
        if(null!=request.getParameter("id")&&"".equals(request.getParameter("id"))){
             id= Integer.parseInt(request.getParameter("id"));
        }
        String drivingLicense=request.getParameter("drivingLicense");
        String fn=request.getParameter("fn");
        int sort=0;
        if(null!=request.getParameter("id")&&"".equals(request.getParameter("id"))){
            sort= Integer.parseInt(request.getParameter("sort"));
        }

        String lpn=request.getParameter("lpn");
        String vin=request.getParameter("vin");
        if (!hasText(guid)) {
            return new Result(Result.FAILURE, "账号编号不能为空");
        }
        if (hasText(lpn) && !hasText(vin)) {
            return new Result(Result.FAILURE, "发动机号不能为空");
        }
        if (hasText(vin) && !hasText(lpn)) {
            return new Result(Result.FAILURE, "车牌号不能为空");
        }
        if (hasText(drivingLicense) && !hasText(fn)) {
            return new Result(Result.FAILURE, "档案编号不能为空");
        }
        if (hasText(fn) && !hasText(drivingLicense)) {
            return new Result(Result.FAILURE, "驾驶证号不能为空");
        }
        //驾驶证进入
        if(hasText(drivingLicense)&&hasText(fn)){
            WtMember wtMember= wtMemberService.getWtMemberByUserGuid(guid);
            String myjszbh="";
            boolean b=false;
                    try {
            myjszbh= DesAPPUtils.decrypt(wtMember.getUserCode());
            if(!(myjszbh.equals(drivingLicense)))
            {
                return new Result(Result.FAILURE,"与本人身份证不相同");
            }
            DriverLicenseSearchCondition driverLicenseSearchCondition = new DriverLicenseSearchCondition();
            driverLicenseSearchCondition.setDabh(fn);
            driverLicenseSearchCondition.setJtglywdxsfzhm(drivingLicense);
            String command = JSON.toJSONString(driverLicenseSearchCondition);
            InQueryServiceInfo inQueryServiceInfo
                    = new InQueryServiceInfo(UUID.randomUUID().toString().replace("-", "")
                    , InQueryServiceInfo.API_010, InQueryServiceInfo.API_ASYNC_Y, command
                    , MD5Util.getMD5String(command), new Date(), request.getRemoteHost(), 0);

            b=  jlApiService.doExecute(inQueryServiceInfo);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(false==b){
            return new Result(Result.FAILURE,"驾驶证信息输入错误，无法绑定");
        }
            driverInfo.setFn(fn);
            driverInfo.setDrivingLicense(drivingLicense);
            driverInfo.setGuid(guid);
            driverInfo.setId(id);
            driverInfo.setSort(sort);


            //驾驶证信息属实，进入绑定

                try {
                    if("".equals(id)||0==id){
                  id= driverInfoMapper.findDriverId(guid);
                    }
                    driverInfoMapper.removeDriverBind(id);
                    driverInfoMapper.bindDriverInfo(driverInfo);
                }catch (Exception e)
                {
                    return new Result(Result.FAILURE, "信息绑定失败");
                }
                return new Result(Result.SUCC,driverInfo.getId(),"绑定驾驶证成功");


        }
        //车辆绑定进入
        if (hasText(lpn) && hasText(vin)) {

        }

        return new Result(Result.FAILURE,"绑定失败");
    }

    @Override
    public Result removeDriverBindInfo(int pk) {
        if (0 == pk)
            return new Result(Result.FAILURE, "请选择要移除车辆信息");
        int rt = driverInfoMapper.removeDriverBind(pk);
        if (rt > 0)
            return new Result("移除绑定信息成功");
        return new Result(Result.FAILURE, "移除绑定信息失败");
    }

    @Override
    public Result loadVehicleBindInfo(String userId) {
        if (Strings.isNullOrEmpty(userId)) {
            return new Result(Result.FAILURE, "请选择要查询用户编号");
        }
        List<DriverInfos> list = driverInfoMapper.loadVehicleBindInfo(userId);
        if (list.isEmpty())
            return new Result(Result.FAILURE, "暂无绑定信息");
        return new Result(list);
    }
}
